package com.qfedu.dao.impl;

import com.qfedu.dao.CategoryDao;
import com.qfedu.pojo.Category;
import com.qfedu.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CategoryDaoImpl implements CategoryDao {
    JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public int deleteCateById(String cid) {
        String sql="delete from category where cid=?";
        int rows = jdbcTemplate.update(sql, cid);
        return rows;
    }

    @Override
    public int updateCategory(Category category,String cid) {
        String sql="update category set cid=?,cname=? where cid=?";
        int rows = jdbcTemplate.update(sql, category.getCid(), category.getCname(),cid);
        return rows;
    }

    @Override
    public int createCategory(Category category) {
        String sql="insert into category values(?,?)";
        int rows = jdbcTemplate.update(sql, category.getCid(), category.getCname());
        return rows;
    }

    @Override
    public List<Category> findAllCategory() {
        String sql="select * from category";
        List<Category> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Category.class));
        return list;
    }

    @Override
    public Category fidCateById(String id) {
        String sql="select * from category where cid=?";
        List<Category> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Category.class), id);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
